let app = getApp()

import Notify from '@vant/weapp/notify/notify';

import {
    selfFormatTimeYMD,
    selfFormatTimeHM,
    twoTimeIntervalReturnHours
} from "../../../utils/util.js"

import {
    selectReservationByIdRq,
    selectCoordinateRq,
    roomContentIsVisitorRq,
    roomContentAddVisitorRq,
    inviteRecordPersonListRq
} from "../../../api/meeting/meetingRoom.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        mapData: {
            // latitude: 33.601291,
            // longitude: 119.031829,
            // markers: [{
            //     id: 1,
            //     latitude: 33.601291,
            //     longitude: 119.031829,
            //     title: '淮安茂业天地购物中心'
            // }]
        },
        id: null,
        detail: null,
        joinPersonList: [],
        address: {},
        participateShow: false,
        openDoorShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        _this.setData({
            ...options
        })
    },

    // 获取地址信息
    getAddress(parkId) {
        let _this = this;
        selectCoordinateRq({
            parkId
        }).then(res => {
            _this.setData({
                address: res,
                mapData: {
                    latitude: res.lat,
                    longitude: res.lng,
                    markers: [{
                        id: 1,
                        latitude: res.lat,
                        longitude: res.lng,
                        title: res.address,
                    }]
                },
            })
        })
    },

    // 获取详情
    getDetail() {
        let _this = this;
        let id = _this.data.id;
        selectReservationByIdRq(id).then(res => {
            let detail = res.data;
            // 时间段
            detail.timeSlot = selfFormatTimeYMD(detail.startTime) + ' ' + selfFormatTimeHM(detail.startTime) + '~' + selfFormatTimeHM(detail.endDate);
            _this.setData({
                detail
            })
            // 获取地址信息
            _this.getAddress(detail.parkId)
            // 查询参与会议预约人员列表
            _this.getInviteRecordPersonList()
            // 判断是否参与过会议
            _this.whetherParticipate()
        })
    },

    // 查询参与会议预约人员列表
    getInviteRecordPersonList() {
        let _this = this;
        let id = _this.data.id;
        inviteRecordPersonListRq(id).then(res => {
            console.log('inviteRecordPersonListRq', res);
            _this.setData({
                joinPersonList: res.data
            })
        })
    },
    // 打开地图
    openMap(e) {
        console.log('openMap', e);
        let _this = this;
        wx.openLocation({
            name: _this.data.address.address,
            latitude: _this.data.address.lat,
            longitude: _this.data.address.lng,
        })
    },

    // 判断是否参与过会议
    whetherParticipate() {
        let _this = this
        roomContentIsVisitorRq({
            "userId": _this.data.detail.userId,
            "participantId": wx.getStorageSync('userId'),
            "reservationId": _this.data.detail.id,
        }).then(res => {
            console.log('whetherParticipate', res);
            if (res.code == 0) { // 未预约
                _this.setData({
                    participateShow: true
                })
            } else { // 已预约
                _this.setData({
                    openDoorShow: true
                })
            }
        })
    },

    // 参与会议
    participateMeeting() {
        let _this = this;
        roomContentAddVisitorRq({
            "userId": _this.data.detail.userId,
            "participantId": wx.getStorageSync('userId'),
            "reservationId": _this.data.detail.id,
            "participantPhone": wx.getStorageSync('user').mobile
        }).then(res => {
            console.log('participateMeeting', res);
            if (res.code == 0) { //参与成功
                _this.setData({
                    participateShow: false,
                    openDoorShow: true
                })
                // 查询参与会议预约人员列表
                _this.getInviteRecordPersonList()
            } else { //参与失败
                Notify({
                    type: 'danger',
                    message: res.msg
                });
            }
        })
    },

    // 开门
    openDoor() {
        let _this = this;
        wx.reLaunch({
            url: "/pages/meeting/accessControl/accessControl",
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        let userId = wx.getStorageSync('userId');
        if (userId) {
            // 获取详情
            _this.getDetail();
        } else {
            app.getlogin()
            return
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})